package cn.com.wdjedu.androidapp.wjeduandroid.ui.introduceui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.List;

import cn.com.wdjedu.androidapp.wjeduandroid.Login;
import cn.com.wdjedu.androidapp.wjeduandroid.R;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.Constanct;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.mainfragment.MainBean;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.videoshow.VideoShowActivity;

public class VideoGridViewAdapter extends CommonAdapter<MainBean.CourseBean.ContentBean> {

    private String title[];

   // private MainBean.CourseBean.JavaBean clickbean;
    public VideoGridViewAdapter(Context context, int layoutId, List<MainBean.CourseBean.ContentBean> datas) {
        super(context, layoutId, datas);
    }


    @Override
    protected void convert(ViewHolder viewHolder, final MainBean.CourseBean.ContentBean item, int position) {
      //  viewHolder.setImageResource(R.id.item_image_icon,R.drawable.ic_launcher);
        title=item.getTitle().split("-");
        ImageLoader.getInstance().displayImage(Constanct.IMAGE_URL2 +item.getImage(), (ImageView) viewHolder.getView(R.id.item_image_icon));
        viewHolder.setText(R.id.item_name,title[0]);
        viewHolder.setText(R.id.item_unit_name,title[1]);
        viewHolder.setText(R.id.item_price,String.valueOf(item.getPrice())+"￥");
        viewHolder.setText(R.id.item_study_number,item.getStudy_number()+"人学习");

        viewHolder.setOnClickListener(R.id.myUnitItemLayout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( Constanct.USER_TOKEN != "") {
                    // Toast.makeText(mContext,"点击的是！："+item.getName(),Toast.LENGTH_LONG).show();
                    CourseIntroduceActivity activity = (CourseIntroduceActivity) mContext;
                    Intent intent = new Intent(activity.getApplication(), VideoShowActivity.class);
                    //intent.putExtra("videoBean",item);
//                intent.putExtra("videopath",item.getVideo_path());
//                intent.putExtra("videoUnit",String.valueOf(item.getUnit()));
//                intent.putExtra("videoName",item.getName());
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", item.getId());//传递播放视频的ID，用于初始化章节显示
                    intent.putExtras(bundle);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    Constanct.PLAYING_URL = item.getVideo_path();
                    Constanct.PLAYING_NAME = item.getName();
                    activity.getApplication().startActivity(intent);
                }
                else{
                Intent intent2 = new Intent(mContext, Login.class);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                Toast.makeText(mContext,"观看免费视频之前请先登录(⊙o⊙)哦",Toast.LENGTH_LONG).show();
                mContext.startActivity(intent2);
            }

            }
        });
}
}
